package com.cloudcc.boot.entity.sap;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.Map;

@Configuration
@PropertySource(value = "classpath:sap-enums.properties", encoding = "UTF-8")
@ConfigurationProperties(prefix = "sap.enums")
@Data
public class SapEnumsProperties {

    private Map<String, String> orderType;

    private Map<String, String> salesOrg;

    private Map<String, String> salesChannel;

    private Map<String, String> productGroup;

    public String getOrderTypeCode(String name) {
        return orderType.entrySet().stream()
                .filter(entry -> entry.getValue().equals(name))
                .map(Map.Entry::getKey)
                .findFirst()
                .orElseThrow(() -> new RuntimeException("未找到订单类型:%s对应的编码,请检查配置文件 - sap-enums.properties".formatted(name)));
    }

    public String getSalesOrgCode(String name) {
        return salesOrg.entrySet().stream()
                .filter(entry -> entry.getValue().equals(name))
                .map(Map.Entry::getKey)
                .findFirst()
                .orElseThrow(() -> new RuntimeException("未找到销售组织:%s对应的编码,请检查配置文件 - sap-enums.properties".formatted(name)));
    }

    public String getSalesChannelCode(String name) {
        return salesChannel.entrySet().stream()
                .filter(entry -> entry.getValue().equals(name))
                .map(Map.Entry::getKey)
                .findFirst()
                .orElseThrow(() -> new RuntimeException("未找到销售渠道:%s对应的编码,请检查配置文件 - sap-enums.properties".formatted(name)));
    }

    public String getProductGroupCode(String name) {
        return productGroup.entrySet().stream()
                .filter(entry -> entry.getValue().equals(name))
                .map(Map.Entry::getKey)
                .findFirst()
                .orElseThrow(() -> new RuntimeException("未找到产品组:%s对应的编码,请检查配置文件 - sap-enums.properties".formatted(name)));
    }

}
